﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Quest
{
    class Player: Mover
    {
        private Weapon equippedWeapon;
        private int hitPoints;
        public int HitPoints { get { return hitPoints; } }
        private int lastHitPoints;
        //public int LastHitPoints { get { return lastHitPoints; } set { lastHitPoints = value; } }

        private List<Weapon> inventory = new List<Weapon>();
        private List<Weapon> lastInventory = new List<Weapon>();
        //        public List<string> Weapons
        public List<Weapons> Weapons
        {
            get
            {
//                List<string> names = new List<string>();
                List<Weapons> names = new List<Weapons>();
                foreach (Weapon weapon in inventory)
                    names.Add(weapon.Name);
                return names;
            }
        }


        public Player(Game game, Point point)
            : base(game, point)
        {
            hitPoints = 10; 
        }


        public void saveCurrentData()
        {
            lastInventory = inventory;
            lastHitPoints = hitPoints;
        }

        public void restoreLastData()
        {
            inventory = lastInventory;
            hitPoints = lastHitPoints;
        }


        public void Hit(int maxDamage, Random random)
        {
            hitPoints -= random.Next(1, maxDamage);
        }

        public void IncreaseHealth(int health, Random random)
        {
            hitPoints += random.Next(1, health);
        }

        //public void Equip(string weaponName)
        //{
        //    foreach (Weapon weapon in inventory)
        //    {
        //        if (weapon.Name == weaponName)
        //            equippedWeapon = weapon;
        //    }
        //}
        // 1st change: using Enumeration
        //public void Equip(Weapons weaponName)
        //{
        //    foreach (Weapon weapon in inventory)
        //        if (weapon.Name == weaponName)
        //            equippedWeapon = weapon;
        //}
        // 2nd change:
        // no need to loop the whole inventory: let's get out as soon as I find the right weapon 
        public void Equip(Weapons weaponName)
        {
            foreach (Weapon weapon in inventory)
                if (weapon.Name == weaponName)
                {
                    equippedWeapon = weapon;
                    return;
                }
        }
        // 3rd possible change
        // why should I bother to loop through inventory to check weaponName?
        // the caller function shold not pass an item that's not in the inventory. let's check it later


        public void Move(Direction direction)// this overloads its abstract class corresponding method, which is not abstract
        {
            //why not simply calling
            //   base.Move(direction, game.Boundaries);
            //and adapting that method with setting location instead of returning it as a Point?
            //I'll check it later
            location = Move(direction, game.Boundaries);
            Weapon WeaponInRoom = game.WeaponInRoom; // added this for smoother coding to avoid typing "game.WeaponInRoom" every time
            if (!WeaponInRoom.PickedUp)
            {
                // see if the weapon is nearby, and possibly pick it up
                if (Nearby(WeaponInRoom.Location, 1))
                {
                    WeaponInRoom.PickUpWeapon();
                    inventory.Add(WeaponInRoom);
                    if (!(equippedWeapon == null) && Weapons.Count==1)
                        Equip(WeaponInRoom.Name);
                }
            }
        }

        public void Attack(Direction direction, Random random)
        {
            if (equippedWeapon == null)
            {
                MessageBox.Show("You must equip a weapon!");
                return;
            }
            equippedWeapon.Attack(direction, random);
            if (equippedWeapon is IPotion)
                inventory.Remove(equippedWeapon);

        }




    }
}
